package com.example.xinwenkehuduan.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xinwenkehuduan.MyTestBean;
import com.example.xinwenkehuduan.R;

import java.util.List;


public class MyNewsAdapter extends BaseAdapter {
    List<MyTestBean> datalist;
    Context context;

    public MyNewsAdapter(List<MyTestBean> datalist, Context context) {
        this.datalist = datalist;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datalist != null ? datalist.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return datalist.get(position).getType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;

        //先获取当前页面的类型
        int Type = getItemViewType(position);
        if (convertView == null) {
            switch (Type){
                case 0:
                    //加载第一个页面
                    holder1=new ViewHolder1();
                    convertView= LayoutInflater.from(context).inflate(R.layout.item1,null);
                    convertView.setTag(holder1);
                    break;
                case 1:
                    //加载第二个页面
                    holder2=new ViewHolder2();
                    convertView= LayoutInflater.from(context).inflate(R.layout.item2,null);
                    convertView.setTag(holder2);
                    break;
            }
        }else {
            switch (Type){
                case 0:
                    holder1= (ViewHolder1) convertView.getTag();
                    break;
                case 1:
                    holder2= (ViewHolder2) convertView.getTag();
                    break;
            }
        }
        return convertView;
    }

    class ViewHolder1 {
        ImageView imageViewA;
        TextView tvTitleA, tvDataA, tvAuthorA;
    }

    class ViewHolder2 {
        ImageView imageViewB1, imageViewB2, imageViewB3;
        TextView tvTitleB, tvDataB, tvAuthorB;
    }
}
